package com.run.runbbs.controller;

import com.run.runbbs.Event.EventProducer;
import com.run.runbbs.common.lang.Result;
import com.run.runbbs.entity.Event;
import com.run.runbbs.entity.Post;
import com.run.runbbs.entity.User;
import com.run.runbbs.service.impl.LikeService;
import com.run.runbbs.service.impl.PostService;
import com.run.runbbs.service.impl.UserService;
import com.run.runbbs.util.Constant;
import com.run.runbbs.util.JwtUtils;
import com.run.runbbs.util.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
public class PostController implements Constant {
    @Autowired
    private PostService postService;
    @Autowired
    private UserService userService;
    @Autowired
    private LikeService likeService;
    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private EventProducer eventProducer;

    @GetMapping("/getPosts")
    public Result getPosts(int offset){
        List<Post> list=postService.findPosts(0, offset, 10);
        List<Map<String,Object>> posts=new ArrayList<>();
        if (list!=null){
            for(Post post:list){
                Map<String,Object> map=new HashMap<>();

                //截取一部分帖子内容
                if(post.getContent().length()>80){
                    post.setContent(StrUtils.getTextFromHtml(post.getContent()));
                }
                map.put("post",post);
                //添加帖子作者信息
                User user=userService.findSimpleUserById(post.getUserId());
                map.put("user",user);
                //添加帖子赞的数量
                long likeCount=likeService.findEntityLikeCount(ENTITY_TYPE_POST,post.getId());
                map.put("likeCount",likeCount);
                posts.add(map);
            }
        }
        return Result.success(posts);
    }
    @GetMapping("/getPostDetail")
    public Result getPostDetail(int postId){
        Post post=postService.findPost(postId);
        Map<String,Object> map=new HashMap<>();
        if(post!=null){
            map.put("post",post);
            //添加帖子作者信息
            User user=userService.findSimpleUserById(post.getUserId());
            map.put("user",user);
            //添加帖子赞的数量
            long likeCount=likeService.findEntityLikeCount(ENTITY_TYPE_POST,post.getId());
            map.put("likeCount",likeCount);
        }


        return Result.success(map);
    }


    @GetMapping("/getPostsRows")
    public Result getPostsRows(){
        int rows=postService.findPostRows(0);
        return Result.success(rows);
    }

    @GetMapping("/getUserPosts")
    public Result getUserPosts(int userId,int offset){
        List<Post> list=postService.findPosts(userId, offset, 10);
        List<Map<String,Object>> posts=new ArrayList<>();
        if (list!=null){
            for(Post post:list){
                Map<String,Object> map=new HashMap<>();
                //截取一部分帖子内容
                if(post.getContent().length()>80){
                    post.setContent(post.getContent().substring(0,80));
                }
                map.put("post",post);
                //添加帖子作者信息
                User user=userService.findSimpleUserById(post.getUserId());
                map.put("user",user);
                //添加帖子赞的数量
                long likeCount=likeService.findEntityLikeCount(ENTITY_TYPE_POST,post.getId());
                map.put("likeCount",likeCount);
                posts.add(map);
            }
        }
        return Result.success(posts);
    }

    @PostMapping("/addPost")
    public Result addPost(String title,String content, HttpServletRequest request){
        int userId=jwtUtils.getUserId(request);

        Post post=new Post();
        post.setUserId(userId);
        post.setTitle(title);
        post.setContent(content);
        post.setCreateTime(new Date());
        postService.addPost(post);
        //触发发帖事件（将帖子同步到es服务器）这样处理是为了异步。
        Event event=new Event()
                .setTopic(TOPIC_PUBLISH)
                .setUserId(userId)
                .setEntityType(ENTITY_TYPE_POST)
                .setEntityId(post.getId());
        eventProducer.fireEvent(event);
        return Result.success("发布成功！");
    }



}
